<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户名" prop="username">
      <el-input v-model="dataForm.username" placeholder="用户名"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input v-model="dataForm.password" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="sex">
      <el-input v-model="dataForm.sex" placeholder="性别"></el-input>
    </el-form-item>
    <el-form-item label="生日" prop="birthday">
      <el-input v-model="dataForm.birthday" placeholder="生日"></el-input>
    </el-form-item>
    <el-form-item label="地址" prop="address">
      <el-input v-model="dataForm.address" placeholder="地址"></el-input>
    </el-form-item>
    <el-form-item label="邮箱" prop="email">
      <el-input v-model="dataForm.email" placeholder="邮箱"></el-input>
    </el-form-item>
    <el-form-item label="qq" prop="qq">
      <el-input v-model="dataForm.qq" placeholder="qq"></el-input>
    </el-form-item>
    <el-form-item label="电话" prop="tel">
      <el-input v-model="dataForm.tel" placeholder="电话"></el-input>
    </el-form-item>
    <el-form-item label="账户余额" prop="money">
      <el-input v-model="dataForm.money" placeholder="账户余额"></el-input>
    </el-form-item>
    <el-form-item label="头像" prop="thumb">
      <el-input v-model="dataForm.thumb" placeholder="头像"></el-input>
    </el-form-item>
    <el-form-item label="" prop="grade">
      <el-input v-model="dataForm.grade" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="类型" prop="type">
      <el-input v-model="dataForm.type" placeholder="类型"></el-input>
    </el-form-item>
    <el-form-item label="注册时间" prop="created">
      <el-input v-model="dataForm.created" placeholder="注册时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updated">
      <el-input v-model="dataForm.updated" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="" prop="age">
      <el-input v-model="dataForm.age" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="ecard">
      <el-input v-model="dataForm.ecard" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="company">
      <el-input v-model="dataForm.company" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="realname">
      <el-input v-model="dataForm.realname" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="position">
      <el-input v-model="dataForm.position" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="signature">
      <el-input v-model="dataForm.signature" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="pid">
      <el-input v-model="dataForm.pid" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="cid">
      <el-input v-model="dataForm.cid" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="aid">
      <el-input v-model="dataForm.aid" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="vip">
      <el-input v-model="dataForm.vip" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="jf">
      <el-input v-model="dataForm.jf" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          username: '',
          password: '',
          sex: '',
          birthday: '',
          address: '',
          email: '',
          qq: '',
          tel: '',
          money: '',
          thumb: '',
          grade: '',
          type: '',
          created: '',
          updated: '',
          status: '',
          age: '',
          ecard: '',
          company: '',
          realname: '',
          position: '',
          signature: '',
          pid: '',
          cid: '',
          aid: '',
          vip: '',
          jf: ''
        },
        dataRule: {
          username: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '密码不能为空', trigger: 'blur' }
          ],
          sex: [
            { required: true, message: '性别不能为空', trigger: 'blur' }
          ],
          birthday: [
            { required: true, message: '生日不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '地址不能为空', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '邮箱不能为空', trigger: 'blur' }
          ],
          qq: [
            { required: true, message: 'qq不能为空', trigger: 'blur' }
          ],
          tel: [
            { required: true, message: '电话不能为空', trigger: 'blur' }
          ],
          money: [
            { required: true, message: '账户余额不能为空', trigger: 'blur' }
          ],
          thumb: [
            { required: true, message: '头像不能为空', trigger: 'blur' }
          ],
          grade: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '类型不能为空', trigger: 'blur' }
          ],
          created: [
            { required: true, message: '注册时间不能为空', trigger: 'blur' }
          ],
          updated: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          age: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          ecard: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          company: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          realname: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          position: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          signature: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          pid: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          cid: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          aid: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          vip: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          jf: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/hotel/users/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.username = data.users.username
                this.dataForm.password = data.users.password
                this.dataForm.sex = data.users.sex
                this.dataForm.birthday = data.users.birthday
                this.dataForm.address = data.users.address
                this.dataForm.email = data.users.email
                this.dataForm.qq = data.users.qq
                this.dataForm.tel = data.users.tel
                this.dataForm.money = data.users.money
                this.dataForm.thumb = data.users.thumb
                this.dataForm.grade = data.users.grade
                this.dataForm.type = data.users.type
                this.dataForm.created = data.users.created
                this.dataForm.updated = data.users.updated
                this.dataForm.status = data.users.status
                this.dataForm.age = data.users.age
                this.dataForm.ecard = data.users.ecard
                this.dataForm.company = data.users.company
                this.dataForm.realname = data.users.realname
                this.dataForm.position = data.users.position
                this.dataForm.signature = data.users.signature
                this.dataForm.pid = data.users.pid
                this.dataForm.cid = data.users.cid
                this.dataForm.aid = data.users.aid
                this.dataForm.vip = data.users.vip
                this.dataForm.jf = data.users.jf
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/hotel/users/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'username': this.dataForm.username,
                'password': this.dataForm.password,
                'sex': this.dataForm.sex,
                'birthday': this.dataForm.birthday,
                'address': this.dataForm.address,
                'email': this.dataForm.email,
                'qq': this.dataForm.qq,
                'tel': this.dataForm.tel,
                'money': this.dataForm.money,
                'thumb': this.dataForm.thumb,
                'grade': this.dataForm.grade,
                'type': this.dataForm.type,
                'created': this.dataForm.created,
                'updated': this.dataForm.updated,
                'status': this.dataForm.status,
                'age': this.dataForm.age,
                'ecard': this.dataForm.ecard,
                'company': this.dataForm.company,
                'realname': this.dataForm.realname,
                'position': this.dataForm.position,
                'signature': this.dataForm.signature,
                'pid': this.dataForm.pid,
                'cid': this.dataForm.cid,
                'aid': this.dataForm.aid,
                'vip': this.dataForm.vip,
                'jf': this.dataForm.jf
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
